package com.example.springboot.controller;

import com.example.springboot.common.Result;

import com.example.springboot.controller.request.BorrowPageRequest;
import com.example.springboot.entity.Borrow;
import com.example.springboot.service.IBorrowService;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/borrow")
public class BorrowController {

    @Resource
    private IBorrowService borrowService;

    @GetMapping("/list")
    public Result list(){
        List<Borrow> borrow = borrowService.list();
        return Result.success(borrow);
    }
    @GetMapping("/page") //分页查询 借阅信息表数据
    public Result page(BorrowPageRequest borrowPageRequest){
        return Result.success(borrowService.page(borrowPageRequest));
    }

    @PutMapping("/return/{recordId}")//定义return接口归还书籍
    public Result returnBook(@PathVariable int recordId) {
        borrowService.updateStatus(recordId, 1); // 1 表示已归还
        return Result.success("书籍已归还");
    }
    @PostMapping("/addBorrow")//定义借阅记录接口来插入借阅数据
    public Result addBorrow(@RequestBody Borrow borrow) {
        borrowService.addBorrow(borrow);
        return Result.success();
    }
    @GetMapping("/todayBorrowRate") //定义todayBorrowRate接口来获取当日借阅率
    public Result getTodayBorrowRate() {
        double rate = borrowService.getTodayBorrowRate();
        return Result.success(rate);
    }
}